﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace quizzer_linq_3
{

    /**Class: LoginScreen
     * @author Samuel McCravy
     * @version 1.1
     * Course : ITEC 4860 Spring 2012
     * Written: March 22, 2012
     * 
     * This Class checks usernames and passwords to make sure they are correct and then allows access to the correct menu.
     * 
     * Purpose: The purpose of this class is to make sure guests login to the correct screen so they can play quizzer and that admins get to the admin screen and that
     * their creditials are verified.
    */

    public partial class LoginScreen : Form
    {
        //This is a constant used to lookup professor's email for login purposes

        const int PROFESSOR_EMAIL_LOOKUP_INDEX = 3;

        //DataAccess is the variable for using the database

        Quizzer_Data_Access dataAccess = new Quizzer_Data_Access();

        public LoginScreen()
        {
            InitializeComponent();
        }

        /** Method: loginbutton_Click
         * Checks login creditials
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */

        private void loginButton_Click(object sender, EventArgs e)
        {
            //This gets the username from the username textbox on the form

            string username = (string)textBoxUsername.Text;

            //This gets the password from the psasword textbox on the form

            string password = (string)textBoxPassword.Text;

            //Guest account is for all students to just play the game, does not have access to the admin panels 
            if (username.Equals("Guest") && password.Equals("Guest"))
            {
                loginGuest();
            }

            //Admin\Admin is the basic admin in case you need to access admin panel in case everyone gets locked out
            else if (username.Equals("Admin") && password.Equals("Admin"))
            {
                loginAdmin();
            }

            else if (username.Length > 0 && password.Length > 0)
            {
                loginProfessor(username, password);
            }
            else
            {
                //This shows this message if there is no match to username and password

                MessageBox.Show("Incorrect Password or Username", "Error!");
            }

            textBoxPassword.Text = "";
        }

        /**Method: loginProfessor
         * 
         * Logs in as a professor and goes to the admin login screen - the username is the professor's email address
         * @param
         */

        private void loginProfessor(string username, string password)
        {
            //Pulls all the professors from the database and puts them into they're own array

            String[] getAllProfessors = dataAccess.GetAllProfessor().Split('\n');
            for (int counter = 0; counter < getAllProfessors.Length - 1; counter++)
            {
                //Gets all the professors from the other array and seperates there entries into a new array

                String[] checkUsername = ((String)(getAllProfessors.GetValue(counter))).Split('|');

                //Trims the password from the database to make sure there are no extra spaces

                string trimPassword = ((string)(checkUsername.GetValue(4))).Trim();
                if (((string)checkUsername.GetValue(PROFESSOR_EMAIL_LOOKUP_INDEX)).ToUpper().Equals(username.ToUpper()) &&
                    trimPassword.Equals(password))
                {
                    //Sends professors to the admin screen

                    this.Hide();
                    AdminLogin userLogin = new AdminLogin(int.Parse((String)checkUsername.GetValue(0)));
                    userLogin.Text = "User Login";

                    userLogin.ShowDialog();
                    this.Show();
                    break;
                }
                else
                {
                    if (counter == getAllProfessors.Length - 2)
                    {
                        //This shows this message if there is no match to username and password

                        MessageBox.Show("Incorrect Password or Username", "Error!");
                    }
                }
            }
        }

        /**Method: loginGuest
         * 
         * Logs in as a guest
         */

        private void loginGuest()
        {
            //Sends students/guests to the menu to setup a game
            this.Hide();
            Game_Setup studentLogin = new Game_Setup();
            studentLogin.ShowDialog();
            this.Show();
        }

        /**Method: loginAdmin
         * 
         * Logs in as an Admin
         */

        private void loginAdmin()
        {
            //Sends admins to the admin screen
            this.Hide();
            AdminLogin adminLogin = new AdminLogin();
            adminLogin.ShowDialog();
            this.Show();
        }

        /** Method: exitButton_Click
         * Closes the application
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */
        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /** Method: textBoxUsername_Enter
         * When the textBoxUsername is focused, select all of the text.
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */

        private void textBoxUsername_Enter(object sender, EventArgs e)
        {
            textBoxUsername.SelectAll();
        }
    }
}
